home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap05 / CtlDemo5 / CtlDemo5.cpp next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  4.4 KB  |  153 lines

  1. //***********************************************************************
  2. //
  3. //  CtlDemo5.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "CtlDemo5.h"
  10.  
  11. #define IDC_RED     100
  12. #define IDC_GREEN   101
  13. #define IDC_BLUE    102
  14.  
  15. CMyApp myApp;
  16.  
  17. /////////////////////////////////////////////////////////////////////////
  18. // CMyApp member functions
  19.  
  20. BOOL CMyApp::InitInstance ()
  21. {
  22.     m_pMainWnd = new CMainWindow;
  23.     m_pMainWnd->ShowWindow (m_nCmdShow);
  24.     m_pMainWnd->UpdateWindow ();
  25.     return TRUE;
  26. }
  27.  
  28. /////////////////////////////////////////////////////////////////////////
  29. // CMainWindow message map and member functions
  30.  
  31. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  32.     ON_WM_CREATE ()
  33.     ON_BN_CLICKED (IDC_RED, OnRedButtonClicked)
  34.     ON_BN_CLICKED (IDC_GREEN, OnGreenButtonClicked)
  35.     ON_BN_CLICKED (IDC_BLUE, OnBlueButtonClicked)
  36. END_MESSAGE_MAP ()
  37.  
  38. CMainWindow::CMainWindow ()
  39. {
  40.     CString strWndClass = AfxRegisterWndClass (
  41.         0,
  42.         myApp.LoadStandardCursor (IDC_ARROW),
  43.         (HBRUSH) (COLOR_3DFACE + 1),
  44.         myApp.LoadStandardIcon (IDI_APPLICATION)
  45.     );
  46.  
  47.     Create (strWndClass, "CtlDemo5");
  48. }
  49.  
  50. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  51. {
  52.     if (CFrameWnd::OnCreate (lpcs) == -1)
  53.         return -1;
  54.  
  55.     CClientDC dc (this);
  56.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
  57.  
  58.     m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  59.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  60.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  61.  
  62.     CFont* pOldFont = dc.SelectObject (&m_font);
  63.     TEXTMETRIC tm;
  64.     dc.GetTextMetrics (&tm);
  65.     m_cxChar = tm.tmAveCharWidth;
  66.     m_cyChar = tm.tmHeight + tm.tmExternalLeading;
  67.     dc.SelectObject (pOldFont);
  68.  
  69.     m_ctlGroupBox1.Create ("Sample text", WS_CHILD | WS_VISIBLE |
  70.         BS_GROUPBOX, CRect (m_cxChar * 2, m_cyChar, m_cxChar * 62,
  71.         m_cyChar * 8), this, UINT (-1));
  72.  
  73.     m_ctlText.Create ("Click a button to change my color",
  74.         WS_CHILD | WS_VISIBLE | SS_CENTER, CRect (m_cxChar * 4,
  75.         m_cyChar * 4, m_cxChar * 60, m_cyChar * 6), this);
  76.  
  77.     m_ctlGroupBox2.Create ("Color", WS_CHILD | WS_VISIBLE |
  78.         BS_GROUPBOX, CRect (m_cxChar * 64, m_cyChar, m_cxChar * 80,
  79.         m_cyChar * 8), this, UINT (-1));
  80.  
  81.     m_ctlRadioButtonRed.Create ("Red", WS_CHILD | WS_VISIBLE | WS_GROUP |
  82.         BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, m_cyChar * 3,
  83.         m_cxChar * 78, m_cyChar * 4), this, IDC_RED);
  84.  
  85.     m_ctlRadioButtonGreen.Create ("Green", WS_CHILD | WS_VISIBLE |
  86.         BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, (m_cyChar * 9) / 2,
  87.         m_cxChar * 78, (m_cyChar * 11) / 2), this, IDC_GREEN);
  88.  
  89.     m_ctlRadioButtonBlue.Create ("Blue", WS_CHILD | WS_VISIBLE |
  90.         BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, m_cyChar * 6,
  91.         m_cxChar * 78, m_cyChar * 7), this, IDC_BLUE);
  92.  
  93.     m_ctlRadioButtonRed.SetCheck (1);
  94.     m_ctlText.SetTextColor (RGB (255, 0, 0));
  95.  
  96.     m_ctlGroupBox1.SetFont (&m_font, FALSE);
  97.     m_ctlGroupBox2.SetFont (&m_font, FALSE);
  98.     m_ctlRadioButtonRed.SetFont (&m_font, FALSE);
  99.     m_ctlRadioButtonGreen.SetFont (&m_font, FALSE);
  100.     m_ctlRadioButtonBlue.SetFont (&m_font, FALSE);
  101.     return 0;
  102. }
  103.  
  104. void CMainWindow::OnRedButtonClicked ()
  105. {
  106.     m_ctlText.SetTextColor (RGB (255, 0, 0));
  107. }
  108.  
  109. void CMainWindow::OnGreenButtonClicked ()
  110. {
  111.     m_ctlText.SetTextColor (RGB (0, 255, 0));
  112. }
  113.  
  114. void CMainWindow::OnBlueButtonClicked ()
  115. {
  116.     m_ctlText.SetTextColor (RGB (0, 0, 255));
  117. }
  118.  
  119. /////////////////////////////////////////////////////////////////////////
  120. // CColorStatic message map and member functions
  121.  
  122. BEGIN_MESSAGE_MAP (CColorStatic, CStatic)
  123.     ON_WM_CTLCOLOR_REFLECT ()
  124. END_MESSAGE_MAP ()
  125.  
  126. CColorStatic::CColorStatic ()
  127. {
  128.     m_crTextColor = RGB (0, 0, 0);
  129.     m_crBkColor = ::GetSysColor (COLOR_3DFACE);
  130.     m_brBkgnd.CreateSolidBrush (m_crBkColor);
  131. }
  132.  
  133. void CColorStatic::SetTextColor (COLORREF crColor)
  134. {
  135.     m_crTextColor = crColor;
  136.     Invalidate ();
  137. }
  138.  
  139. void CColorStatic::SetBkColor (COLORREF crColor)
  140. {
  141.     m_crBkColor = crColor;
  142.     m_brBkgnd.DeleteObject ();
  143.     m_brBkgnd.CreateSolidBrush (crColor);
  144.     Invalidate ();
  145. }
  146.  
  147. HBRUSH CColorStatic::CtlColor (CDC* pDC, UINT nCtlColor)
  148. {
  149.     pDC->SetTextColor (m_crTextColor);
  150.     pDC->SetBkColor (m_crBkColor);
  151.     return (HBRUSH) m_brBkgnd;
  152. }
  153.